package com.comp3111.happymeal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.object.*;
import com.configuration.Configuration;
import com.controller.*;

public class FoodPageUserReviewActivity extends Activity {
	ListView list;
	MealUserCommentAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.food_page_user_review);
        Bundle extras = getIntent().getExtras();
//        createDrawer();
//      Get Data from intent
      int mealId = Integer.parseInt(extras.getString("MEALID"));	//Check out discoverPageActivity onClickListener
      Log.d("TAT", extras.getString("MEALID"));
   
		ArrayList<HashMap<String, String>> mealCommentList = new ArrayList<HashMap<String, String>> ();

		//Getting the meal Comment from server
		MealCommentFetch mealCommentFetch = new MealCommentFetch();
        UserFetch userFetch = new UserFetch();
        MealFetch mealFetch = new MealFetch();

        
//        List<MealComment> mealCommentListDB = mealCommentFetch.fetchMealCommentByMealId(mealId);
        List<MealComment> mealCommentListDB = mealCommentFetch.fetchAllMealComment();
        if(mealCommentListDB != null){
    	   if(Configuration.showLog){
    		   Log.d("FoodPageUserReview",Integer.toString(mealCommentListDB.size()));
    	   }
    	   
        for(MealComment eachMealComment : mealCommentListDB){
			HashMap<String, String> map = new HashMap<String, String> ();
			map.put("userName", userFetch.fetchUserNameById(eachMealComment.getUser()));
			map.put("mealName", mealFetch.fetchMealNameById(eachMealComment.getMeal()));
            map.put("mealComment",eachMealComment.getComment());
            map.put("mealCreateDate",eachMealComment.
            		getCreateDate());   
            mealCommentList.add(map);
     	   if(Configuration.showLog){

			Log.d("userName", userFetch.fetchUserNameById(eachMealComment.getUser()));
			Log.d("mealName", mealFetch.fetchMealNameById(eachMealComment.getMeal()));
			Log.d("mealComment",eachMealComment.getComment());
			Log.d("mealCreateDate",eachMealComment.getCreateDate());
     	   }
        }
        
//        List<MealComment> mealCommentList2 = mealCommentFetch.fetchAllMealComment();
//        for(MealComment eachMealComment : mealCommentList2){
//        	Log.d("DiscoverPageActivity","==============EACH RESTAURANT COMMENT==============");
//        	Log.d("DiscoverPageActivity",userFetch.fetchUserNameById(eachMealComment.getUser()));
//            Log.d("DiscoverPageActivity",mealFetch.fetchMealNameById(eachMealComment.getMeal()));
//            Log.d("DiscoverPageActivity",eachMealComment.getComment());
//            Log.d("DiscoverPageActivity",eachMealComment.getCreateDate());
//            
//        }
        

		list = (ListView)findViewById(R.id.food_page_user_review_main_list);

		// Getting adapter by passing xml data ArrayList
		adapter=new MealUserCommentAdapter(this, mealCommentList);
		list.setAdapter(adapter);
		
			// Click event for single list row
			list.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			}
			});
			
        }else{
     	   Log.d("FoodPageUserReview","null");
        }
        
	}
}
